package com.qf.sportsevent.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.MenuItem;

import com.alibaba.fastjson.JSON;
import com.qf.sportsevent.R;
import com.qf.sportsevent.adapter.CommonRecyclerAdapter;
import com.qf.sportsevent.databinding.ActivityNewsSpecialBinding;
import com.qf.sportsevent.entity.NewsContentsEntity;
import com.qf.sportsevent.entity.NewsContestEntity;
import com.qf.sportsevent.entity.NewsContestListEntity;
import com.qf.sportsevent.entity.NewsSpecialChildsEntity;
import com.qf.sportsevent.entity.NewsSpecialDataEntity;
import com.qf.sportsevent.entity.NewsSpecialEntity;
import com.qf.sportsevent.util.FastEventBus;
import com.qf.sportsevent.util.HttpUtils;
import com.qf.sportsevent.util.RecyclerItemLayoutManager;
import com.qf.sportsevent.util.UrlManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class NewsSpecialActivity extends AppCompatActivity implements Callback {
    private ActivityNewsSpecialBinding binding;
    private String url;
    private CommonRecyclerAdapter<Object> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        binding = DataBindingUtil.setContentView(this, R.layout.activity_news_special);
        setSupportActionBar(binding.toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        int id = getIntent().getIntExtra("id", -1);
        if(id!=-1){
            url = UrlManager.getSpecialNewsUrl(id);
            adapter = new CommonRecyclerAdapter<>(this,new ArrayList(), RecyclerItemLayoutManager.getLayoutMap());
            binding.recycler.setAdapter(adapter);
            HttpUtils.okHttpAsyncGet(url,this);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onFailure(Call call, IOException e) {}
    @Override
    public void onResponse(Call call, Response response) throws IOException {
        String jsonStr = response.body().string();
        NewsSpecialEntity specialEntity = JSON.parseObject(jsonStr,NewsSpecialEntity.class);
        FastEventBus bus = new FastEventBus();
        bus.setNewsSpecialEntity(specialEntity);
        EventBus.getDefault().post(bus);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onBusArrived(FastEventBus bus){
        NewsSpecialEntity entity = bus.getNewsSpecialEntity();
        if (entity != null) {
            if(entity.getCode()==200){
                NewsSpecialDataEntity dataEntity = entity.getData();
                if (dataEntity != null) {
                    binding.setSpecialData(dataEntity);
                    List<String> list = new ArrayList<>();
                    list.add(dataEntity.getTitle());
                    list.add(dataEntity.getText());
                    adapter.add(list);

                    NewsContestListEntity contestListEntity = dataEntity.getContest_content();
                    if (contestListEntity != null) {
                        NewsContestEntity contestEntity = contestListEntity.getContest();
                        if (contestEntity != null) {
                            contestEntity.setSpecial(true);
                            adapter.add(contestEntity);
                        }
                    }

                    List<NewsSpecialChildsEntity> childsEntities = dataEntity.getChilds();
                    if (childsEntities != null && childsEntities.size() > 0) {
                        for (int i = 0; i < childsEntities.size(); i++) {
                            NewsSpecialChildsEntity child = childsEntities.get(i);
                            if (child.getTitle() != null && !TextUtils.isEmpty(child.getTitle())) {
                                adapter.add(child.getTitle());
                            }
                            List<NewsContentsEntity> contents = child.getContents();
                            if (contents != null && contents.size() > 0) {
                                for (int j = 0; j < contents.size(); j++) {
                                    adapter.add(contents.get(j));
                                }
                            }
                        }
                    }
                }
            }else{
                FastEventBus errorBus=new FastEventBus();
                errorBus.setMessage(entity.getMsg());
                EventBus.getDefault().post(errorBus);
                finish();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
